#include <stdio.h>     // 提供 printf、perror 等标准 I/O 函数
#include <unistd.h>    // 提供 pipe、fork、read、write、close 等系统调用
#include <string.h>    // 提供 strlen 函数
#include <stdlib.h>    // 提供 exit 函数
#include <sys/types.h> // 提供 pid_t 类型
#include <sys/wait.h>  // 提供 waitpid 函数
int main()
{
    int fd[2] = {0};
    if (pipe(fd) < 0)
    {
        perror("pipe");
        return 1;
    }
    pid_t id = fork();
    if (id == 0)
    {
        close(fd[0]);
        const char *msg = "hello father,i am child...";
        int count = 10;
        while (count--)
        {
            write(fd[1], msg, strlen(msg));
            sleep(1);
        }
        close(fd[1]);
        exit(0);
    }
    close(fd[1]);
    char buff[64];
    while (1)
    {
        ssize_t s = read(fd[0], buff, sizeof(buff));
        if (s > 0)
        {
            buff[s] = '\0';
            printf("child sent to father:%s\n", buff);
        }
        else if (s == 0)
        {
            printf("read file end\n");
            break;
        }
    }
    close(fd[0]);
    waitpid(id, NULL, 0);
    return 0;
}